package Game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.geom.RoundRectangle2D;

//游戏菜单界面GUI类
//负责创建和展示游戏主菜单界面

public class GameMenuGUI {
    // 菜单面板组件
    private JPanel menuPanel;
    // 最高分显示标签
    private JLabel bestScoreMenuLabel;
    // 最高分记录
    private int bestScore;

    //构造函数
    public GameMenuGUI(int initialBestScore) {
        this.bestScore = initialBestScore;
        createMenuPanel(); // 初始化菜单面板
    }

    //获取菜单面板
    public JPanel getMenuPanel() {
        return menuPanel;
    }

    //更新最高分显示
    public void updateBestScore(int newScore) {
        bestScore = newScore;
        bestScoreMenuLabel.setText("最高分: " + bestScore);
    }

    //创建菜单面板
    private void createMenuPanel() {
        // 创建带有渐变背景的菜单面板
        menuPanel = new JPanel(new GridBagLayout()) {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                // 创建渐变背景
                GradientPaint gradient = new GradientPaint(0, 0, new Color(50, 50, 100),
                        getWidth(), getHeight(), new Color(20, 20, 40));
                g2d.setPaint(gradient);
                g2d.fillRect(0, 0, getWidth(), getHeight());
            }
        };

        // 设置布局约束
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(10, 10, 30, 10);

        // 创建游戏标题标签
        JLabel titleLabel = new JLabel("矿工大冒险");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 48));
        titleLabel.setForeground(new Color(255, 215, 0)); // 金色文字
        menuPanel.add(titleLabel, gbc);

        // 创建游戏规则文本区域
        JTextArea rulesArea = new JTextArea();
        rulesArea.setText("游戏规则:\n\n" +
                "1. 使用方向键或WASD键控制角色移动\n" +
                "2. 碰到矿物会自动挖掘，获得分数但减少耐久度\n" +
                "3. 不同矿物消耗的耐久度不同，价值也不同\n" +
                "4. 碰到'修'字格可以修复矿稿\n" +
                "5. 找到'下'字格可以进入下一层\n" +
                "6. 耐久度归零游戏结束\n\n" +
                "目标：尽可能地挖更多矿换取更高的分数吧！");
        rulesArea.setEditable(false);
        rulesArea.setLineWrap(true); // 自动换行
        rulesArea.setWrapStyleWord(true); // 按单词换行
        rulesArea.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        rulesArea.setForeground(Color.WHITE);
        rulesArea.setOpaque(false); // 透明背景
        rulesArea.setMargin(new Insets(10, 10, 10, 10)); // 设置边距

        // 将规则文本放入滚动面板
        JScrollPane rulesScroll = new JScrollPane(rulesArea);
        rulesScroll.setPreferredSize(new Dimension(500, 200));
        rulesScroll.setOpaque(false);
        rulesScroll.getViewport().setOpaque(false);
        rulesScroll.setBorder(BorderFactory.createEmptyBorder());
        menuPanel.add(rulesScroll, gbc);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 20, 0));
        buttonPanel.setOpaque(false); // 透明背景

        // 创建开始和退出按钮
        JButton startButton = createStyledButton("开始游戏");
        JButton exitButton = createStyledButton("退出游戏");

        // 添加按钮到按钮面板
        buttonPanel.add(startButton);
        buttonPanel.add(exitButton);

        // 调整按钮面板位置
        gbc.insets = new Insets(30, 10, 10, 10);
        menuPanel.add(buttonPanel, gbc);

        // 创建最高分显示标签
        bestScoreMenuLabel = new JLabel("最高分: " + bestScore);
        bestScoreMenuLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
        bestScoreMenuLabel.setForeground(new Color(255, 215, 0)); // 金色文字
        menuPanel.add(bestScoreMenuLabel, gbc);

        // 设置退出按钮事件
        exitButton.addActionListener(e -> System.exit(0));
    }

     //设置开始按钮的监听器
    public void setStartButtonListener(ActionListener listener) {
        // 遍历菜单面板组件查找开始按钮
        Component[] components = menuPanel.getComponents();
        for (Component comp : components) {
            if (comp instanceof JPanel) {
                Component[] buttons = ((JPanel) comp).getComponents();
                for (Component btn : buttons) {
                    if (btn instanceof JButton && ((JButton) btn).getText().equals("开始游戏")) {
                        ((JButton) btn).addActionListener(listener);
                        return;
                    }
                }
            }
        }
    }

    //创建样式化按钮
    private JButton createStyledButton(String text) {
        // 自定义按钮绘制
        JButton button = new JButton(text) {
            @Override
            protected void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                // 根据按钮状态设置不同颜色
                if (getModel().isPressed()) {
                    g2.setColor(new Color(100, 100, 150)); // 按下状态
                } else if (getModel().isRollover()) {
                    g2.setColor(new Color(80, 80, 130)); // 悬停状态
                } else {
                    g2.setColor(new Color(60, 60, 100)); // 正常状态
                }

                // 绘制圆角矩形背景
                g2.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), 20, 20));
                g2.setColor(Color.WHITE);
                g2.setFont(new Font("微软雅黑", Font.BOLD, 18));
                // 居中绘制文本
                FontMetrics fm = g2.getFontMetrics();
                int x = (getWidth() - fm.stringWidth(getText())) / 2;
                int y = (getHeight() - fm.getHeight()) / 2 + fm.getAscent();
                g2.drawString(getText(), x, y);
            }
        };

        // 设置按钮属性
        button.setContentAreaFilled(false);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setPreferredSize(new Dimension(200, 50));
        return button;
    }
}